package com.dianrui.operation.util;

import com.amap.api.maps.model.LatLng;

import java.math.BigDecimal;
import java.util.List;

public class GaodeUtils {
    public static final double MIN_LAT = -90;
    public static final double MAX_LAT = 90;
    public static final double MIN_LNG = -180;
    public static final double MAX_LNG = 180;


    public static class Poi{
        private Double lat;
        private Double lng;
        public Poi(Double lng,Double lat){
            this.lat = lat;
            this.lng = lng;
        }

    }

    /**
     * 计算经纬度围成的实际面积（平方公里）
     * @return
     */
    public static BigDecimal getArea(List<Poi> ring){
        double sJ = 6378137;
        double Hq = 0.017453292519943295;
        double c = sJ *Hq;
        double d = 0;

        if (3 > ring.size()) {
            return new BigDecimal( 0);
        }

        for (int i = 0; i < ring.size() - 1; i ++){
            Poi h = ring.get(i);
            Poi k = ring.get(i + 1);
            double u = h.lng * c * Math.cos(h.lat * Hq);

            double hhh = h.lat * c;
            double v = k.lng * c * Math.cos(k.lat *Hq);
            d = d + (u * k.lat * c - v * hhh);
        }

        Poi g1 = ring.get(ring.size()-1);
        Poi point = ring.get(0);
        double eee = g1.lng * c * Math.cos(g1.lat * Hq);
        double g2 = g1.lat * c;

        double k = point.lng * c * Math.cos(point.lat * Hq);
        d += eee * point.lat * c - k * g2;
        return new BigDecimal( 0.5*Math.abs(d)).divide(new BigDecimal(1000000));
    }

    // 经度最小值
    public static double getMinLongitude(List<LatLng> mPoints) {
        double minLongitude = MAX_LNG;
        if (mPoints.size() > 0) {
            minLongitude = mPoints.get(0).longitude;
            for (LatLng latlng : mPoints) {
                // 经度最小值
                if (latlng.longitude < minLongitude)
                    minLongitude = latlng.longitude;
            }
        }
        return minLongitude;
    }

    // 经度最大值
    public static double getMaxLongitude(List<LatLng> mPoints) {
        double maxLongitude = MIN_LNG;
        if (mPoints.size() > 0) {
            maxLongitude = mPoints.get(0).longitude;
            for (LatLng latlng : mPoints) {
                // 经度最大值
                if (latlng.longitude > maxLongitude)
                    maxLongitude = latlng.longitude;
            }
        }
        return maxLongitude;
    }

    // 纬度最小值
    public static double getMinLatitude(List<LatLng> mPoints) {
        double minLatitude = MAX_LAT;
        if (mPoints.size() > 0) {
            minLatitude = mPoints.get(0).latitude;
            for (LatLng latlng : mPoints) {
                // 纬度最小值
                if (latlng.latitude < minLatitude)
                    minLatitude = latlng.latitude;
            }
        }
        return minLatitude;
    }

    // 纬度最大值
    public static double getMaxLatitude(List<LatLng> mPoints) {
        double maxLatitude = MIN_LAT;
        if (mPoints.size() > 0) {
            maxLatitude = mPoints.get(0).latitude;
            for (LatLng latlng : mPoints) {
                // 纬度最大值
                if (latlng.latitude > maxLatitude)
                    maxLatitude = latlng.latitude;
            }
        }
        return maxLatitude;
    }



    public static LatLng getCenterOfGravityPoint(List<LatLng> mPoints) {
        double area = 0.0;//多边形面积
        double Gx = 0.0, Gy = 0.0;// 重心的x、y
        for (int i = 1; i <= mPoints.size(); i++) {
            double iLat = mPoints.get(i % mPoints.size()).latitude;
            double iLng = mPoints.get(i % mPoints.size()).longitude;
            double nextLat = mPoints.get(i - 1).latitude;
            double nextLng = mPoints.get(i - 1).longitude;
            double temp = (iLat * nextLng - iLng * nextLat) / 2.0;
            area += temp;
            Gx += temp * (iLat + nextLat) / 3.0;
            Gy += temp * (iLng + nextLng) / 3.0;
        }
        Gx = Gx / area;
        Gy = Gy / area;
        return new LatLng(Gx, Gy);
    }


    public static LatLng getCenterOfGravityPoints(List<LatLng> mPoints) {
        double area = 0.0;//多边形面积
        double Gx = 0.0, Gy = 0.0;// 重心的x、y
        for (int i = 1; i <= mPoints.size(); i++) {
            double iLat = mPoints.get(i % mPoints.size()).latitude;
            double iLng = mPoints.get(i % mPoints.size()).longitude;
            double nextLat = mPoints.get(i - 1).latitude;
            double nextLng = mPoints.get(i - 1).longitude;
            double temp = (iLat * nextLng - iLng * nextLat) / 2.0;
            area += temp;
            Gx += temp * (iLat + nextLat) / 3.0;
            Gy += temp * (iLng + nextLng) / 3.0;
        }
        Gx = Gx / area+0.00480277796198;
        Gy = Gy / area+0.0029329000086;
        return new LatLng(Gx, Gy);
    }





}
